package com.heima.article.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author lvFF
 * @Date 2022/06/12/19:30;
 * Description
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Override
    public ResponseResult load(Short loadtypeLoadMore, ArticleHomeDto dto) {
        //1.校验参数
        //检查dto分页size是否为空
        if (dto.getSize()==null || dto.getSize()==0){
            //不对
            dto.setSize(10);
        }
        int min = Math.min(dto.getSize(), MAX_PAGE_SIZE);
        dto.setSize(min);
        //类型参数检验
        //判断是否上刷或者下刷
        if (!loadtypeLoadMore.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!loadtypeLoadMore.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            loadtypeLoadMore=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //文章频道校验
        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        //时间校验
        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        if (dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        //2.查询数据
        List<ApArticle> list = apArticleMapper.loadArticleList(dto,loadtypeLoadMore);
        //3.结果封装
        ResponseResult result = ResponseResult.okResult(list);
        return result;
    }
}
